package com.example.pclimb.repository;

import android.app.Application;

import androidx.lifecycle.LiveData;

import com.example.pclimb.dao.ReviewDao;
import com.example.pclimb.database.PclimbDatabase;
import com.example.pclimb.model.Review;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 评价数据仓库，处理评价相关的所有数据操作
 */
public class ReviewRepository {
    private ReviewDao reviewDao;
    private ExecutorService executorService;

    public ReviewRepository(Application application) {
        PclimbDatabase database = PclimbDatabase.getInstance(application);
        reviewDao = database.reviewDao();
        executorService = Executors.newSingleThreadExecutor();
    }

    // 添加评价
    public void insert(Review review, UserRepository.Callback<Long> callback) {
        executorService.execute(() -> {
            long id = reviewDao.insert(review);
            callback.onResult(id);
        });
    }

    // 更新评价
    public void update(Review review) {
        executorService.execute(() -> {
            reviewDao.update(review);
        });
    }

    // 删除评价
    public void delete(Review review) {
        executorService.execute(() -> {
            reviewDao.delete(review);
        });
    }

    // 获取特定评价
    public LiveData<Review> getReviewById(int reviewId) {
        return reviewDao.getReviewById(reviewId);
    }

    // 获取用户的所有评价
    public LiveData<List<Review>> getReviewsByUser(int userId) {
        return reviewDao.getReviewsByUserId(userId);
    }

    // 获取陪爬人员的所有评价
    public LiveData<List<Review>> getReviewsByGuide(int guideId) {
        return reviewDao.getReviewsByGuideId(guideId);
    }

    // 获取陪爬请求的评价
    public LiveData<Review> getReviewByRequest(int requestId) {
        return reviewDao.getReviewByRequestId(requestId);
    }

    // 获取陪爬人员的平均评分
    public LiveData<Float> getAverageRatingForGuide(int guideId) {
        return reviewDao.getAverageRatingForGuide(guideId);
    }

    // 获取陪爬人员的评价数量
    public LiveData<Integer> getReviewCountForGuide(int guideId) {
        return reviewDao.getReviewCountForGuide(guideId);
    }
} 